import logging
import unittest
import requests
import utils
from api.login import LoginApi
from api.recharge import RechargeApi
from api.tender import TenderApi
from api.trust_account import TrustAccountApi
from parameterized import parameterized


class TestTenderFlow(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.session = requests.session()
        cls.login_api = LoginApi(cls.session)
        cls.approve_realname = TrustAccountApi(cls.session)
        cls.recharge_api = RechargeApi(cls.session)
        cls.tender_api = TenderApi(cls.session)

        # 清理数据库垃圾数据
        sql1 = "delete from mb_member where name = '13203003591'"
        sql2 = "delete from mb_member_info where member_name = '13203003591'"
        sql3 = "delete from mb_member_register_log where member_name = '13203003591'"
        utils.DBUtil.update_one(sql1, sql2, sql3)

    @classmethod
    def tearDownClass(cls) -> None:
        cls.session.close()

    # 获取图片验证码成功（随机值r为随机整数1）
    def test01_img_verify_code(self):
        # 测试数据
        r = 1

        # 调用接口
        response = self.login_api.get_img_verify_code(r)

        # 断言
        self.assertEqual(200, response.status_code)

        # 输入日志
        logging.info(f"获取图片验证码的结果为：{response}")

    # 获取短信验证码失败（图片验证码错误）
    def test02_info_verify_code(self):
        # 测试数据
        phone = "13203003591"
        img_verify_code = "8888"

        # 调用接口
        response = self.login_api.get_info_verify_code(phone, img_verify_code)

        # 断言
        utils.common_assert(self, response, 200, 200, "短信发送成功")

        # 输出日志
        logging.info(f"获取短信验证码的结果为：{response.text}")

    # 注册成功（填入所有参数）
    def test03_register_success_all_params(self):
        # 测试数据
        phone = "13203003591"
        password = "hyc123456"
        img_verify_code = "8888"
        phone_code = "666666"
        dy_server = "on"
        invite_phone = "13203003592"

        # 调用接口
        res_register = self.login_api.register(phone, password, img_verify_code, phone_code, dy_server, invite_phone)

        # 断言
        utils.common_assert(self, res_register, 200, 200, "注册成功")

        # 输出日志
        logging.info(f"注册结果为：{res_register.json()}")

    # 登录成功（用户名和密码输入正确）
    def test04_login_success(self):
        keywords = "13203003591"
        password = "hyc123456"

        # 调用接口
        res_login = self.login_api.login(keywords, password)

        # 断言
        utils.common_assert(self, res_login, 200, 200, "登录成功")

        # 输出日志
        logging.info(f"登录结果为：{res_login.json()}")

        # 实名认证成功（输入正确的信息和身份证）

    def test05_approve_realname(self):
        # 测试数据
        realname = "李恩"
        card_id = "110101199003075816"

        # 调用接口
        res_approve_realname = self.approve_realname.approve_realname(realname, card_id)

        # 断言
        utils.common_assert(self, res_approve_realname, 200, 200, "提交成功")

        # 输出日志
        logging.info("实名认证的结果为：{}".format(res_approve_realname.json()))

    # 开户成功
    def test06_trust_register(self):

        # 调用接口
        res_trust_register = self.approve_realname.trust_register()

        # 断言
        utils.common_assert(self, res_trust_register, 200, 200, None)

        # 输出日志
        logging.info("实名认证的结果为：{}".format(res_trust_register.json()))

        # 提取开户接口的响应数据
        html = res_trust_register.json()["description"].get("form")

        # 调用第三方函数提取数据
        action, form_data = utils.parse_html(html)

        # 输出日志
        logging.info("提取的结果为：{}".format(form_data))

        # 调用第三方开户接口
        res_trust_register_third = self.session.post(action, data=form_data)
        # 断言
        self.assertEqual(200, res_trust_register_third.status_code)
        # 输出日志
        logging.info("第三方开户接口返回的结果为：{}".format(res_trust_register_third.text))

    # 获取充值验证码成功（r为1）
    def test07_get_recharge_code_success(self):
        # 调用接口
        res_get_recharge_code = self.recharge_api.get_recharge_code(1)

        # 断言
        self.assertEqual(200, res_get_recharge_code.status_code)

        # 输出日志
        logging.info("获取充值验证码的结果为：{}".format(res_get_recharge_code.text))

    # 获取充值成功
    def test08_get_recharge_success(self):

        # 调用接口
        res_get_recharge_code = self.recharge_api.get_recharge_code(1)
        res_get_recharge = self.recharge_api.get_recharge("chinapnrTrust", 70000, "reForm", 8888)

        # 提取数据
        html = res_get_recharge.json()["description"].get("form")
        print(html)
        action, data_form = utils.parse_html(html)
        res_get_recharge_third = self.session.post(url=action, data=data_form)

        # 断言
        self.assertEqual(200, res_get_recharge_code.status_code)
        utils.common_assert(self, res_get_recharge, 200, 200, None)
        self.assertEqual(200, res_get_recharge_third.status_code)
        self.assertIn("OK", res_get_recharge_third.text)

        # 输出日志
        logging.info("获取充值验证码的结果为：{}".format(res_get_recharge_code.text))
        logging.info("获取充值的结果为：{}".format(res_get_recharge.json()))
        logging.info("获取第三方充值的结果为：{}".format(res_get_recharge_third.text))

    @parameterized.expand(utils.read_file_freedom("tender", "desc,id,amount,status,status_code"))
    def test09_tender(self, desc, id, amount, status_code, status):
        # 调用投资接口
        res_tender = self.tender_api.get_tender(id, amount)

        # 公共断言
        utils.common_assert(self, res_tender, status_code, status, None)

        # 输出日志
        logging.info(f"投资的结果为：{res_tender.text}")

    def test10_tender_third(self):
        # 调用投资接口
        res_tender = self.tender_api.get_tender(2059, 500)
        # 提取相关数据
        html = res_tender.json()["description"].get("form")
        actions, form_data = utils.parse_html(html)

        # 调用第三方投资接口
        res_tender_third = self.session.post(url=actions, data=form_data)

        # 公共断言
        utils.common_assert(self, res_tender, 200, 200, None)
        self.assertEqual(200, res_tender_third.status_code)
        self.assertIn("OK", res_tender_third.text)

        # 输出日志
        logging.info(f"投资的结果为：{res_tender.text}")
        logging.info(f"第三方投资的结果为：{res_tender_third.text}")